package net.mingsoft.zwy.biz.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.mingsoft.basic.util.RestTemplateUtil;
import net.mingsoft.basic.util.UniqueCodeUtil;
import net.mingsoft.msend.util.MailUtil;
import net.mingsoft.report_data.util.Md5Utils;
import net.mingsoft.zwy.biz.MessageSendBiz;
import net.mingsoft.zwy.biz.q.EmailQuery;
import net.mingsoft.zwy.biz.q.MsgQuery;
import net.mingsoft.zwy.biz.r.SendMsgResponse;
import net.mingsoft.zwy.config.properties.EmailProperty;
import net.mingsoft.zwy.config.properties.MessageProperty;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * @author gengfa
 * @date 22/02/16
 */
@Service
@Slf4j
public class MessageSendBizImpl implements MessageSendBiz {

    @Resource
    MessageProperty property;

    @Resource
    EmailProperty emailProperty;

    private static final String MSG_TYPE_CAPTCHA = "CAPTCHA";

    private static final String LOWEST_PRIORITY = "1";

    @Override
    public SendMsgResponse sendMsg(MsgQuery query) {
        String code = UniqueCodeUtil.genCode();
        query.setJbjgbm(property.getJbjgbm())
                .setApId(property.getApId())
                .setDxId(property.getApId() + code)
                .setDxType(MSG_TYPE_CAPTCHA)
                .setSign(property.getSign())
                .setPriority(LOWEST_PRIORITY);
        query.setMac(makeMacSecret(query));
        final String json = JSON.toJSONString(query);
        final String b64String = Base64.encodeBase64String(json.getBytes(StandardCharsets.UTF_8));
        final String postUrl = MessageFormatter.format(getEnv(property), b64String).getMessage();
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        ResponseEntity<String> response = RestTemplateUtil.post(postUrl, headers, "{}", String.class);
        log.info(response.getBody());
        return JSON.parseObject(response.getBody(), SendMsgResponse.class);
    }

    @Override
    public void sendEmail(EmailQuery query) {
        MailUtil.sendText(emailProperty.getHost(), emailProperty.getPort(), emailProperty.getAccount(), emailProperty.getPassword(),"【海南智库信息平台】邮箱安全验证", query.getContent(), new String[]{query.getTo()});
        log.info("{} {}", query, emailProperty);
    }

    private String makeMacSecret(MsgQuery query) {
        String s = query.getJbjgbm() + query.getApId() + property.getSecretKey() + query.getDxId() + query.getDxType() + query.getMobiles() + query.getContent() + property.getSign() + query.getPriority();
        return Md5Utils.md5Encode(s, StandardCharsets.UTF_8.name());
    }

    private String getEnv(MessageProperty property) {
        if (StringUtils.equals(property.getEnv(), "vlan")) {
            return property.getVlan();
        }
        return property.getInternet();
    }
}
